package com.tencent.wxcloudrun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tencent.wxcloudrun.common.utils.PageUtils;
import com.tencent.wxcloudrun.common.utils.R;
import com.tencent.wxcloudrun.model.BrandEntity;
import com.tencent.wxcloudrun.model.SpuInfoEntity;
import com.tencent.wxcloudrun.service.BrandService;
import com.tencent.wxcloudrun.service.SpuInfoService;
import com.tencent.wxcloudrun.vo.SpuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * spu信息
 *
 * @author xinzi
 * @email 1228604898@qq.com
 * @date 2021-04-14 19:48:30
 */
@RestController
@RequestMapping("/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private BrandService brandService;


    @GetMapping("/skuId/{id}")
    public R SpuInfoBySkuId(@PathVariable("id") Long skuId) {
        SpuInfoEntity spuInfoEntity = spuInfoService.getSpuInfoBySkuId(skuId);

        return R.ok().put("data", spuInfoEntity);
    }


    /**
     * 列表
     */
    //spu检索
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    @GetMapping("/list/{categoryId}")
    public R listByCategoryId(@PathVariable("categoryId") Long categoryId) {
        List<SpuInfoEntity> spuInfoEntities = spuInfoService.getBaseMapper()
                .selectList(new QueryWrapper<SpuInfoEntity>().eq("category_id", categoryId));
        Map<Long, BrandEntity> brandMap = brandService.getAllAsMap();
        List<SpuInfoVO> result = spuInfoEntities.stream().map(e -> {
            BrandEntity brandEntity = brandMap.get(e.getBrandId());
            SpuInfoVO spuInfoVO = new SpuInfoVO();
            spuInfoVO.setSpuInfo(e);
            spuInfoVO.setBrandName(brandEntity.getName());
            spuInfoVO.setBrandDesc(brandEntity.getDescription());
            spuInfoVO.setBrandLogo(brandEntity.getLogo());
            return spuInfoVO;
        }).collect(Collectors.toList());

        return R.ok().put("list", result);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuInfoEntity spuInfoEntity){
		spuInfoService.save(spuInfoEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
